﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace BackOffice
{
    public partial class Advertisement_Maintenance2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int ID = Convert.ToInt32(Request.QueryString["ID"]);
                ViewState["ID"] = ID;
                listAdvertisementDetail(ID);
            }
        }

        protected void listAdvertisementDetail(int ID)
        {
            DataTable dt = Maintenance.listAdvertisement2(ID);

            if (dt.Rows.Count > 0)
            {
                AdvImage.ImageUrl = dt.Rows[0][6].ToString();
                lblVendorName.Text = dt.Rows[0][1].ToString();
                lblDateApply.Text = dt.Rows[0][2].ToString();
                lblDateApproved.Text = dt.Rows[0][3].ToString();
                lblDateExpired.Text = dt.Rows[0][4].ToString();
                lblApprovedBy.Text = dt.Rows[0][5].ToString();
                txtPeriod.Text = dt.Rows[0][9].ToString();
                txtPicSize.Text = dt.Rows[0][10].ToString();
                lblPrice.Text = dt.Rows[0][11].ToString();
                rbStatus.SelectedValue = dt.Rows[0][7].ToString();
                ViewState["DefaultStatus"] = dt.Rows[0][7].ToString();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int ID = Convert.ToInt32(ViewState["ID"]);
            int defaultStatus = Convert.ToInt32(ViewState["DefaultStatus"]);
            int status = Convert.ToInt32(rbStatus.SelectedValue);
            string name = "Admin";
            int period = Convert.ToInt32(txtPeriod.Text);
            int picSize = Convert.ToInt32(txtPicSize.Text);
            //string name = Session["username"].ToString();

            if ((defaultStatus == 1 || defaultStatus == 3) && status == 2)
            {
                int result = 0;
                result = Maintenance.approvedAdvertisement(ID, name, period);

                if (result > 0)
                {
                    //Send mail inform vendor
                    MessageBox.Show("Approved!");
                }
            }

            if ((defaultStatus == 1 || defaultStatus == 2) && status == 3)
            {
                int result = 0;
                result = Maintenance.cancelAdvertisement(ID);

                if (result > 0)
                {
                    //Send mail
                    MessageBox.Show("Cancelled!");
                }
            }


        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Advertisement_Maintenance.aspx");
        }
    }
}